﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Domain.Model;
using Domain.Model.Interfaces;

using NHibernate;
using NHibernate.Engine;
using NHibernate.Criterion;

using DataAccess.Utils;
using DataAccess.Exceptions;


namespace DataAccess.Repositories
{
    public class ProjectRepository : AbstractRepository<Project>, IProjectRepository
    {
        public ProjectRepository() : 
            base(DataRepositoryException.PROJECT_REPOSITORY, "project") { }

        public void DeleteDetailFromProjects(string name)
        {
            using (ISession session = SessionProvider.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        ISQLQuery query = session.CreateSQLQuery("DELETE FROM ProjectDetails WHERE Name = '" + name + "'");
                        query.ExecuteUpdate();

                        session.Flush();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();

                        throw new DataRepositoryException(e.Message,
                            DataRepositoryException.PROJECT_REPOSITORY,
                            "Could not delete details with the name: " + name);
                    }
                }
            }
        }

        public IList<Project> GetStandaloneProjects()
        {
            using (ISession session = SessionProvider.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        ICriteria criteria = session.CreateCriteria<Project>();
                        criteria.Add(Restrictions.IsNull("Program"));
                        return criteria.List<Project>();
                    }
                    catch (Exception e)
                    {
                        throw new DataRepositoryException(e.Message,
                            DataRepositoryException.PROJECT_REPOSITORY,
                            "Could not get standalone projects");
                    }
                }
            }
        }

        public ProjectTask GetTaskById(int taskId)
        {
            using (ISession session = SessionProvider.OpenSession())
            {
                try
                {
                    return session.Get<ProjectTask>(taskId);
                }
                catch (Exception e)
                {
                    throw new DataRepositoryException(e.Message,
                        ExceptionType, "Could not get the ProjectTask with the id: " + id);
                }
            }
        }

        public void DeleteProjectTask(ProjectTask task)
        {
            using (ISession session = SessionProvider.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        ISQLQuery query = session.CreateSQLQuery("DELETE FROM ProjectTasks WHERE Id = '" + task.Id + "'");
                        query.ExecuteUpdate();

                        session.Flush();
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();

                        throw new DataRepositoryException(e.Message,
                            DataRepositoryException.PROJECT_REPOSITORY,
                            "Could not delete task with the name: " + task.Name);
                    }
                }
            }
        }
    }
}